package cn.li.security.json.handle;

import cn.li.security.json.bean.Result;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 登录成功的JSON处理
 */
public class AuthenticationSuccessJSONHandler extends BaseJSONHandler implements AuthenticationSuccessHandler {


    public static final String LOGIN_SUCCESS_MSG = "LOGIN_SUCCESS";

    public AuthenticationSuccessJSONHandler(ObjectMapper objectMapper) {
        super(objectMapper);
    }

    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {

        Result r = new Result(0, LOGIN_SUCCESS_MSG,authentication.getPrincipal());
        this.writeResult(response,r);
    }
}
